package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.RbacManagerDTO;
import com.woniu.dao.RbacManagerDao;
import com.woniu.entity.RbacManager;
import com.woniu.entity.ResponseEntity;
import com.woniu.service.RbacManagerService;
import com.woniu.utils.SecurityCryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:陈涵
 * @Do:
 * @date: 2022/9/1 23:15
 */
@Service
public class RbacManagerServiceImpl implements RbacManagerService {

    @Autowired
    RbacManagerDao rbacManagerDao;

    @Override
    public ResponseEntity findAll(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage, pageSize);//只要在这里传入了分页信息
        List<RbacManager> all = rbacManagerDao.findAll();
        PageInfo<RbacManager> pageInfo = new PageInfo<>(all);//就能够封装分页信息
        return new ResponseEntity(200,"请求成功！",pageInfo);
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = rbacManagerDao.deleteOne(id);
        if (i>0){
            return new ResponseEntity(200,"成功删除"+id+"这条数据",null);
        }else {
            return new ResponseEntity(500,"删除失败",null);
        }

    }

    @Override
    public ResponseEntity addOne(RbacManager rbacManager) {
        //账号不允许重复
        int count = rbacManagerDao.findCountByName(rbacManager.getAccount());
        if(count>0){
            return new ResponseEntity(500,"新增失败,账号同名！",null);
        }else {
            //数据库没有账号的时候 才允许新增
            String s = SecurityCryUtils.generateEncryptedPassword(rbacManager.getPassword(), rbacManager.getAccount());
            rbacManager.setPassword(s);
            int i = rbacManagerDao.addOne(rbacManager);
            if (i>0){
                return new ResponseEntity(200,"成功新增",null);
            }else {
                return new ResponseEntity(500,"新增失败",null);
            }
        }
    }

    @Override
    public ResponseEntity editOne(RbacManager rbacManager) {
        if (rbacManager.getId()!=0){
            int count = rbacManagerDao.findCountByNameOrId(rbacManager);
            if(count>1){
                return new ResponseEntity(500,"修改失败,账号同名！",null);
            }else{
                int i = rbacManagerDao.editOne(rbacManager);
                if (i>0){
                    return new ResponseEntity(200,"成功修改",null);
                }else {
                    return new ResponseEntity(500,"修改失败",null);
                }
            }
        }else{
            return new ResponseEntity(500,"id不能没有！",null);
        }

    }

    @Override
    public RbacManager findOneByName(String username) {
        return rbacManagerDao.findOneByName(username);
    }

    @Override
    public ResponseEntity editType(int id,String status) {
        int i = rbacManagerDao.editType(id,status);
        if (i>0){
            return new ResponseEntity(200,"成功更新"+id+"这条数据状态",null);
        }else {
            return new ResponseEntity(500,"更新失败",null);
        }
    }

    @Override
    public ResponseEntity initPwd(int id) {
        RbacManager rbacManager = rbacManagerDao.findOneById(id);
        String newPassword = SecurityCryUtils.generateEncryptedPassword("123", rbacManager.getAccount());

        int i = rbacManagerDao.updateInitPwd(id,newPassword);
        if (i>0){
            return new ResponseEntity(200,"成功更新"+id+"这条密码成功",null);
        }else {
            return new ResponseEntity(500,"更新失败",null);
        }
    }

    @Override
    public ResponseEntity updateRole(RbacManagerDTO rbacManagerDTO) {
        if (rbacManagerDTO.getId()!=0){
            int count = rbacManagerDao.findCountByNameOrId(rbacManagerDTO);
            if(count>1){
                return new ResponseEntity(500,"修改失败,账号同名！",null);
            }else{
                String newP = SecurityCryUtils.generateEncryptedPassword(rbacManagerDTO.getPassword(), rbacManagerDTO.getAccount());
                rbacManagerDTO.setPassword(newP);
                int i = rbacManagerDao.updateRole(rbacManagerDTO);
                if (i>0){
                    return new ResponseEntity(200,"成功更新’"+rbacManagerDTO.getAccount()+"‘用户的角色",null);
                }else {
                    return new ResponseEntity(500,"更新失败",null);
                }
            }
        }else{
            return new ResponseEntity(500,"id不能没有！",null);
        }
    }
}
